package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.*;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.Royalty;
import com.gxa.jjty.core.pojo.VBillDetails;
import com.gxa.jjty.core.pojo.VSRoyalty;
import com.gxa.jjty.core.pojo.VUnbalanced;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

/**
 * @author asus
 */
@RestController
@RequestMapping("/bill")
@Api(tags = "对账管理")
@Log4j2
public class BillController {

    @Autowired
    BillService billService;

    @Autowired
    VBillDetailsService vBillDetailsService;

   @Autowired
   VUnbalancedService vUnbalancedService;


    @Autowired
    VSRoyaltyService vsRoyaltyService;

    @Autowired
    RoyaltyService royaltyService;

    @PostMapping("/getBill")
    @ApiOperation("查询对账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间")
    })
    public Map selectBill( Integer page,
                          Integer size,
                          String startTime,
                          String endTime
    ) throws ParseException {

        //每天的收益和支出
        List inAndOut = billService.selectInAndOut( startTime, endTime);

        //每天的余额
        List dayBalance = billService.selectDayBalance( startTime, endTime);
        List list = new ArrayList<>();

        int pre = (page-1)*size;
        int end = pre+size-1;


        for (int i = pre; i <=pre+end; i++) {
            Map<String, Object> map = new HashMap<>();
            if (inAndOut.size()>i ){
                map.put("inAndOut",inAndOut.get(i));
                map.put("dayBalance",dayBalance.get(i));
                list.add(map);
            }

        }

       JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
       jsonResult.put("total",inAndOut.size());
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getBillMan")
    @ApiOperation("对账单管理")
    public  Map selectBillManagement(){

        //平台现在的余额
        Double nowBalance = billService.selectNowBalance();
        //待结算金额
        List<VUnbalanced> vUnbalanceds = vUnbalancedService.selectAll();
        //平台收益
        Double totalRevenue = billService.selectTotalRevenue();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("nowBalance",nowBalance);
        map.put("vUnbalanceds",vUnbalanceds);
        map.put("totalRevenue",totalRevenue);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }



    @PostMapping("/getBillD")
    @ApiOperation("查询账单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间"),
            @ApiImplicitParam(name = "name",value = "残疾人名字"),
            @ApiImplicitParam(name = "payType",value = "支付方式"),
            @ApiImplicitParam(name = "orderNumber",value = "订单号")
    })
    public Map selectBillD( Integer page,
                            Integer size,
                           Date endTime,
                            Date startTime,
                           String name,
                            String payType,
                           String orderNumber
                           ){
        PageInfo<VBillDetails> vBillDetailsPageInfo = vBillDetailsService.selectVBillDetails(page, size, startTime, endTime, orderNumber, name, payType);
        List<VBillDetails> billDList = vBillDetailsPageInfo.getList();
        long total = vBillDetailsPageInfo.getTotal();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.setData(billDList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getRoyalty")
    @ApiOperation("查询提成结算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "startTime",value = "开始时间",required = false),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = false),
            @ApiImplicitParam(name = "phyName",value = "陪护员姓名",required = false),
            @ApiImplicitParam(name = "royaltyStatus",value = "结算状态",required = false),
    })
    public Map selectRoyalty( Integer page,
                              Integer size,
                              String startTime,
                              String endTime,
                              String phyName,
                              Integer royaltyStatus
                             ){
        PageInfo<VSRoyalty> vsRoyaltyPageInfo = vsRoyaltyService.selectRoyalty(page, size, startTime, endTime, phyName, royaltyStatus);
        long total = vsRoyaltyPageInfo.getTotal();
        List<VSRoyalty> royaltyList = vsRoyaltyPageInfo.getList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.setData(royaltyList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/setRoyalty")
    @ApiOperation("结算")
    public Map updateRoyalty(@RequestParam Long phyId) throws BusinessException {

        royaltyService.updateRoyalty(phyId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("结算成功");
        return jsonResult.getDataMap();
    }

}
